package com.syl.ippool.api;

import com.syl.ippool.ApplicationStart;
import com.syl.ippool.domain.IpBasePojo;
import com.syl.ippool.utils.WebResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

/**
 * 对外提供获得ip接口的 api类
 *
 * @author sunyalong
 * @date 2019-12-12 21:06:49
 */
@Controller("/api")
@ResponseBody
public class IpPoolApi {


    /**
     * 获得指定数目ip
     *
     * @param num ip数量
     * @return
     */
    @RequestMapping("getByNum")
    public WebResult get(@RequestParam("num") int num) {
        if (num <= 0 || num >= 51) {
            return WebResult.err("num 必须大于等0，小于50");
        }
//        List<IpBasePojo> ipBasePojos = ApplicationStart.setIPPool.get(num);
        List<IpBasePojo> ipBasePojos = ApplicationStart.setIPPool.removeGet(num);
        return WebResult.ok(ipBasePojos);
    }


    /**
     * 获得ip池总数
     *
     * @return
     */
    @RequestMapping("getIpPoolSize")
    public WebResult getIpPoolSize() {
        HashMap<Object, Object> res = new HashMap<>();
        res.put("poolSize", ApplicationStart.setIPPool.getPoolSize());
        return WebResult.ok(res);
    }

    @RequestMapping("test")
    public WebResult test() {

        return WebResult.err("异常");
    }

}
